﻿/**********************************天使物流*******************************************
* 功能描述:物资管理——入库管理
* 作者:孟帅龙
* 时间:2022/1/20
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.Materials.NewLibrarys;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.Materials.NewLibrarys
{
    public class RuKuRepository : DapperDbContextRepository, IRuKuRepository
    {
        public RuKuRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 入库管理——查询分页显示
        /// <summary>
        /// 入库管理——查询分页显示
        /// </summary>
        /// <param name="GoodsNamen"></param>
        /// <param name="Texture"></param>
        /// <param name="Nationw"></param>
        /// <param name="Province"></param>
        /// <param name="PaymentType"></param>
        /// <param name="Registrant"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewLibraryDto>> GetNewLibraryPageShow(string GoodsNamen, int Texture, int Nationw, int Province, int PaymentType, string Registrant, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<NewLibraryDto> response = new PageResponse<NewLibraryDto>();
            var sql = "select a.Id,a.NewLibraryNumber,a.GoodsNamen,a.PaymentType,a.Typey,a.Texture,a.Specification,a.Nationw,a.Province,a.Numbers,a.Remarkm,a.GrossAmountn,a.Registrant,a.EstablishTimez,a.IsDelete,b.DictionaryName as BName,c.DictionaryName as CName,d.DictionaryName as DName,e.CountryName as EName,f.CountryName as FName from NewLibrary a join Dictionary b on a.PaymentType=b.Id join Dictionary c on a.Typey=c.Id join Dictionary d on a.Texture=d.Id join Country e on a.Nationw=e.Id join Country f on a.Province=f.Id where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(GoodsNamen))
            {
                sql += $" and a.GoodsNamen like '%" + GoodsNamen + "%'";
            }
            if (Texture > 0)
            {
                sql += $" and a.Texture='{Texture}'";
            }
            if (Nationw > 0)
            {
                sql += $" and a.Nationw='{Nationw}'";
            }
            if (Province > 0)
            {
                sql += $" and a.Province='{Province}'";
            }
            if (PaymentType > 0)
            {
                sql += $" and a.PaymentType='{PaymentType}'";
            }
            if (!string.IsNullOrWhiteSpace(Registrant))
            {
                sql += $" and a.Registrant like '%" + Registrant + "%'";
            }
            var list = await this.QueryAsync<NewLibraryDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<NewLibraryDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 入库管理——逻辑删除
        /// <summary>
        /// 入库管理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteNewLibrary(int id)
        {

            var sql = $"update NewLibrary set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        #endregion

        #region 入库管理——批量删除
        /// <summary>
        /// 入库管理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteBatchNewLibrary(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update NewLibrary set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        #endregion

        #region 入库管理——编辑
        /// <summary>
        /// 入库管理——编辑
        /// </summary>
        /// <param name="newLibraryDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateNewLibrary(NewLibraryDto newLibraryDto)
        {
            var sql = $"update NewLibrary set NewLibraryNumber='{newLibraryDto.NewLibraryNumber}',GoodsNamen='{newLibraryDto.GoodsNamen}',PaymentType='{newLibraryDto.PaymentType}',Typey='{newLibraryDto.Typey}',Texture='{newLibraryDto.Texture}',Specification='{newLibraryDto.Specification}',Nationw='{newLibraryDto.Nationw}',Province='{newLibraryDto.Province}',Numbers='{newLibraryDto.Numbers}',Remarkm='{newLibraryDto.Remarkm}',GrossAmountn='{newLibraryDto.GrossAmountn}',Registrant='{newLibraryDto.Registrant}',EstablishTimez='{newLibraryDto.EstablishTimez}',IsDelete='{newLibraryDto.IsDelete}' where Id='{newLibraryDto.Id}'";
            return await ExecuteAsync(sql, newLibraryDto);
        }
        #endregion

        #region 入库管理——添加
        /// <summary>
        /// 入库管理——添加
        /// </summary>
        /// <param name="newLibraryDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddNewLibrary(NewLibraryDto newLibraryDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into NewLibrary values('{id}','{newLibraryDto.GoodsNamen}','{newLibraryDto.PaymentType}','{newLibraryDto.Typey}','{newLibraryDto.Texture}','{newLibraryDto.Specification}','{newLibraryDto.Nationw}','{newLibraryDto.Province}','{newLibraryDto.Numbers}','{newLibraryDto.Remarkm}','{newLibraryDto.GrossAmountn}','{newLibraryDto.Registrant}',getdate(),0)";
            return await ExecuteAsync(sql, newLibraryDto);
        }
        #endregion
    }
}
